package com.sservice.web.controller.admin;

import com.sservice.model.dao.LadingOrderMapper;
import com.sservice.model.vo.LadingOrderVo;
import com.sservice.service.service.ExportService;
import com.sservice.service.service.ILadingOrderService;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.model.entity.LadingOrder;
import com.sservice.web.common.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/admin/ladingOrder")
public class LadingOrderController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private ILadingOrderService ladingOrderService;
    @Autowired
    private LadingOrderMapper ladingOrderMapper;
    @Autowired
    private ExportService exportService;



    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult query(LadingOrderVo arg) {
        return ladingOrderService.byPage(arg);
    }
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public OutputStream export(LadingOrderVo arg, HttpServletRequest request, HttpServletResponse response) {
        arg.setPage(0);
        arg.setSize(Integer.MAX_VALUE);// max value
        List<LadingOrderVo> list = ladingOrderMapper.queryByPage(arg);
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        response.setContentType("application/binary;charset=" + "UTF-8");
        String fileName = Utils.processFileName(request, "待开发票明细Excel");
        response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");// 组装附件名称和格式
        response.setCharacterEncoding("UTF-8");
        String hql = "发票"; // sheet 名称
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");

        List<Map> lm = new ArrayList<Map>();
        for (int i = 0; i < list.size(); i++) {
            Map map = new LinkedHashMap();
            LadingOrderVo ov = list.get(i);
            map.put("提单号", ov.getLadingNo());
            map.put("产品名称", ov.getGoodsName());
            map.put("客户名称", ov.getClienteleName());
            map.put("单价", ov.getPrice());
            map.put("实提数量", ov.getActual());
            map.put("已开票数量", ov.getPaperedNumber());
            map.put("发票状态",
                    BigDecimal.ZERO.compareTo(ov.getPaperedNumber())==0?"未开票":(
                            (BigDecimal.ZERO.compareTo(ov.getPaperedNumber())==-1&&ov.getActual().compareTo(ov.getPaperedNumber())==1)?"部分开票":(
                                    ov.getActual().compareTo(ov.getPaperedNumber())==0?"完全开票":""
                                    )));
            map.put("申请用户", ov.getPaperApplier());
            map.put("申请时间", ov.getPaperApplyTime()!=null?sdf1.format(ov.getPaperApplyTime()):"");
            map.put("公司全称", ov.getPaperCom());
            map.put("税号", ov.getPaperUnitCode());
            BigDecimal total = ov.getPrice().multiply(ov.getNumber());
            BigDecimal taxTotal = Utils.taxTotal(ov.getTaxRatio(), total);
            map.put("不含税金额", total.subtract(taxTotal));
            map.put("税率", ov.getTaxRatio());


            map.put("价税合计金额", total);
            map.put("地址", ov.getPaperAddress());
            map.put("电话", ov.getPaperTel());
            map.put("开户行", ov.getPaperBank());
            map.put("账号", ov.getPaperAccount());
            lm.add(map);
        }
        return exportService.exportExcel(hql, lm, outputStream);

    }



    /***
     * 详情查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public BaseResult get(LadingOrderVo arg) {
        LadingOrder lo = ladingOrderService.selectByPrimaryKey(arg.getId());
        return new BaseResult(true, lo);
    }

    /**
     * 根据订单查询发货单
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryByOrderId/{orderId}", method = RequestMethod.GET)
    public BaseResult queryByOrderId(LadingOrder arg) {
        List<LadingOrderVo> lo = ladingOrderMapper.queryOrderBill(arg);
        return new BaseResult(true, lo);
    }
}
